﻿using UnityEngine;
using UnityEngine.UI;

namespace UnityView
{
    public class TextField : UIView
    {
        public readonly InputField InputComponent;

        public readonly UILabel TextView;

        private TextView _placeHolderTextView;
        public TextView PlaceHolderTextView
        {
            get { return _placeHolderTextView; }
            private set
            {
                _placeHolderTextView = value;
                _placeHolderTextView.TextComponent.fontSize = FontSize;
                _placeHolderTextView.TextAlignment = Alignment;
            }
        }

        public TextAnchor Alignment
        {
            set
            {
                TextView.Alignment = value;
                if (_placeHolderTextView != null)
                {
                    _placeHolderTextView.TextAlignment = value;
                }
            }
            get { return TextView.Alignment; }
        }

        public int FontSize
        {
            set
            {
                TextView.FontSize = value;
                if (_placeHolderTextView != null)
                {
                    _placeHolderTextView.FontSize = value;
                }
            }
            get { return TextView.FontSize; }
        }

        public string PlaceHolder
        {
            set
            {
                if (PlaceHolderTextView == null)
                {
                    PlaceHolderTextView = new TextView(this);
                    FrameFill(PlaceHolderTextView);
                    InputComponent.placeholder = PlaceHolderTextView.TextComponent;
                }
                PlaceHolderTextView.Text = value;
            }
            get
            {
                return PlaceHolderTextView.Text;
            }
        }

        private const string DefaultTextFieldName = "Text Field";
        
        // public TextField()
        // {
        //     InputComponent = UIObject.AddComponent<InputField>();
        //     TextView = new TextView(this);
        //     FrameFill(TextView);
        //
        //     // 关联的Text组件
        //     InputComponent.textComponent = TextView.TextComponent;
        // }

        public TextField(UILayout view, string name = DefaultTextFieldName) : base(view, name)
        {
            InputComponent = UIObject.AddComponent<InputField>();
            TextView = new UILabel(this);
            TextView.Alignment = TextAnchor.MiddleLeft;
            RectFill(TextView);
            TextView.RectTransform.offsetMin = new Vector2(12, 6);
            TextView.RectTransform.offsetMax = new Vector2(-12, -6);
            InputComponent.textComponent = TextView.TextComponent;
        }

        public TextField(GameObject gameObject)
        {

        }
    }
}
